﻿import { STATUS_DIC, SHOW_STATUS_DIC } from "@/utils/const";

export default () => {
  // 生成 value -> label 的映射表
  const toMap = dic => Object.fromEntries(dic.map(i => [i.value, i.label]));
  const STATUS_MAP = toMap(STATUS_DIC);
  const SHOW_STATUS_MAP = toMap(SHOW_STATUS_DIC);

  // 搜索配置
  const searchOption = ref({
    formConfig: {
      title: {
        label: "公告标题",
        component: "el-input",
      },
      // 新增时间
      createTime: {
        label: "新增时间",
        component: "el-date-picker",
        type: "daterange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD",
      },
      // 发送时间
      sendTime: {
        label: "发送时间",
        component: "el-date-picker",
        type: "daterange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD",
      },
      // 编辑时间
      updateTime: {
        label: "编辑时间",
        component: "el-date-picker",
        type: "daterange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD",
      },
      // 操作人员
      updateName: {
        label: "操作人员",
        component: "el-input",
      },
      // 发送状态
      status: {
        label: "发送状态",
        component: "el-select",
        dicData: STATUS_DIC,
        dicProps: { label: "label", value: "value" },
        placeholder: "请选择发送状态",
        clearable: true,
      },
      // 展示状态
      showStatus: {
        label: "展示状态",
        component: "el-select",
        dicData: SHOW_STATUS_DIC,
        dicProps: { label: "label", value: "value" },
        placeholder: "请选择展示状态",
        clearable: true,
      },
    },
  });
  const toYesNo = v => (String(v) === "1" || v === 1 ? "是" : "否");

  const tableOption = ref(
    Utils.tools.deleteSearchConfig({
      // 弹窗宽度
      dialogWidth: "30%",
      // 是否显示编辑按钮
      editBtn: false,
      // 是否显示删除按钮
      delBtn: false,
      // 菜单宽度 单个按钮 52px 按钮容器内边距 8px 例如：4个按钮 = 52 * 4 * 8
      menuWidth: 52 * 4 + 8,
      // 对齐方式
      align: "left",
      column: {
        title: { label: "公告标题", minWidth: 160, display: false },
        targetName: { label: "触达对象", minWidth: 160, display: false },
        // isScroll: { label: "是否滚动", minWidth: 120, display: false, formatter: row => toYesNo(row.isScroll) },
        isPopup: { label: "是否弹出", minWidth: 120, display: false, formatter: row => toYesNo(row.isPopup) },
        createTime: {
          label: "新增时间",
          minWidth: 170,
          type: "datetime",
          valueFormat: "YYYY-MM-DD HH:mm:ss",
          addDisplay: false,
          editDisplay: false,
        },
        sendTime: {
          label: "发送时间",
          minWidth: 170,
          type: "datetime",
          valueFormat: "YYYY-MM-DD HH:mm:ss",
          addDisplay: false,
          editDisplay: false,
        },
        updateTime: {
          label: "最近编辑时间",
          minWidth: 170,
          type: "datetime",
          valueFormat: "YYYY-MM-DD HH:mm:ss",
          addDisplay: false,
          editDisplay: false,
        },
        updateName: {
          label: "操作人员",
          minWidth: 120,
          addDisplay: false,
          editDisplay: false,
        },
        status: {
          label: "发送状态",
          minWidth: 120,
          addDisplay: false,
          editDisplay: false,
          formatter: row => STATUS_MAP[row.status] ?? row.status ?? "-",
        },
        showStatus: {
          label: "展示状态",
          minWidth: 120,
          addDisplay: false,
          editDisplay: false,
          formatter: row => {
            // 未发送或发送失败时，展示状态显示"/"
            const sendStatus = String(row.status || "").toUpperCase();
            if (sendStatus === "INIT" || sendStatus === "FAIL") {
              return "/";
            }
            return SHOW_STATUS_MAP[row.showStatus] ?? row.showStatus ?? "-";
          },
        },
      },
    })
  );
  return { searchOption, tableOption };
};
